package com.ctos.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctos.common.Result;
import com.ctos.entity.Employee;
import com.ctos.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;


@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService service;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request,@RequestBody Employee employee){

        String userPassWord = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes(StandardCharsets.UTF_8));
        String userName = employee.getUsername();

        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername, userName).eq(Employee::getPassword,userPassWord);
        Employee user = service.getOne(qw);

        if (user ==null){
            return Result.error("账号或密码错误");
        }
        if (user.getStatus()!=1){
            return Result.error("此账号已被永久封禁");
        }
        request.getSession().setAttribute("userId", user.getId());


        return Result.success(user);
    }

    /**
     * 员工登出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<String> loginOut(HttpServletRequest request){
       request.getSession().removeAttribute("userId");
        return Result.success("登出成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<Page> pageQuery( Integer page,  Integer pageSize,String name){
        Page<Employee> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        //是否有查询名称
        qw.like(name!=null,Employee::getName,name);
        //修改时间 降序
        qw.orderByDesc(Employee::getUpdateTime);

        service.page(pageInfo,qw);//进行查询后的结果是会放在传入的Page对象内的 返回的也是同一个对象

        return Result.success(pageInfo);


    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping()
    public Result<String > save(HttpServletRequest request,@RequestBody Employee employee){

        //当前用户id
        Long userId = (Long) request.getSession().getAttribute("userId");

        //设置初始密码为身份证后六位
        String idNumber = employee.getIdNumber();
        String password = idNumber.substring(idNumber.length()-6);
        String md5 = DigestUtils.md5DigestAsHex(password.getBytes());

        //设置初始值
        employee.setPassword(md5);
        // employee.setCreateUser(userId);
        // employee.setUpdateUser(userId);
        // employee.setCreateTime(LocalDateTime.now());
        // employee.setUpdateTime(LocalDateTime.now());
        service.save(employee);
        return Result.success("success");
    }

    /**
     * 修改员工
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public Result<String> modifyState(HttpServletRequest request,@RequestBody Employee employee){
        //设置修改时间修改人

        service.updateById(employee);
        return Result.success("success");
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping({"/{id}"})
    public Result getOne( @PathVariable Long id){
        return Result.success(service.getById(id));
    }

}
